
package br.com.aeternu.controleatendimento.manutencao.empresa.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import br.com.aeternu.controleatendimento.common.ui.EasyJFrame;
import br.com.aeternu.controleatendimento.common.ui.TableCellListener;

public class UIEmpresa extends EasyJFrame
{

   private static Logger logger = Logger.getLogger(UIEmpresa.class);
   private static final long serialVersionUID = 1L;
   private JButton btnIncluir;
   private JPanel contentPane;
   private IfcUIEmpresa listener;
   private JScrollPane scrollPane;
   private JTable table;

   Action action = new AbstractAction()
   {

      private static final long serialVersionUID = 1L;

      public void actionPerformed(ActionEvent e)
      {
         TableCellListener tcl = (TableCellListener) e.getSource();
         logger.info("Row   : " + tcl.getRow());
         logger.info("Column: " + tcl.getColumn());
         logger.info("Old   : " + tcl.getOldValue());
         logger.info("New   : " + tcl.getNewValue());

         if (tcl.getOldValue() != tcl.getNewValue())
            atualizaPlanilha(tcl.getRow(), tcl.getColumn(), tcl.getNewValue());
      }
   };

   public UIEmpresa(IfcUIEmpresa listener)
   {
      super("Empresas");

      this.listener = listener;

      new TableCellListener(getTable(), action);

      initComponents();
   }

   public JButton getBtnIncluir()
   {
      if (btnIncluir == null)
      {
         btnIncluir = new JButton("Criar Empresa");
         btnIncluir.addActionListener(new ActionListener()
         {

            public void actionPerformed(ActionEvent e)
            {
               listener.criarNovaEmpresa();
            }
         });
         btnIncluir.setMnemonic('C');
      }
      return btnIncluir;
   }

   public JScrollPane getScrollPane()
   {
      if (scrollPane == null)
      {
         scrollPane = new JScrollPane();
         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
         scrollPane.setViewportView(getTable());
      }
      return scrollPane;
   }

   public JTable getTable()
   {
      if (table == null)
      {
         table = new JTable();
         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         table.setModel(getTableModel());
         table.getColumnModel().getColumn(0).setPreferredWidth(150);
         table.getColumnModel().getColumn(0).setMinWidth(150);
         table.getColumnModel().getColumn(0).setMaxWidth(250);
         table.getColumnModel().getColumn(1).setPreferredWidth(150);
         table.getColumnModel().getColumn(1).setMinWidth(150);
         table.getColumnModel().getColumn(1).setMaxWidth(250);
      }
      return table;
   }

   @Override
   public void sair()
   {
      dispose();
   }

   private void atualizaPlanilha(int row, int column, Object newValue)
   {
      listener.atualizaPlanilha(this, row, column, newValue);
   }

   private DefaultTableModel getTableModel()
   {
      return new DefaultTableModel(
         new Object[][]{
            {null, null},
         },
         new String[]{
            "Empresa", "Tipo"
         })
      {

         private static final long serialVersionUID = 1L;
         Class<?>[] columnTypes = new Class[]{
            String.class, String.class
         };

         public Class<?> getColumnClass(int columnIndex)
         {
            return columnTypes[columnIndex];
         }
      };
   }

   private void initComponents()
   {
      setResizable(false);
      setBounds(100, 100, 450, 400);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      GroupLayout gl_contentPane = new GroupLayout(contentPane);
      gl_contentPane.setHorizontalGroup(
               gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                 .addComponent(getBtnIncluir())
                                 .addContainerGap())
                        .addComponent(getScrollPane(), Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
               );
      gl_contentPane.setVerticalGroup(
               gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                                 .addComponent(getScrollPane(), GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                                 .addPreferredGap(ComponentPlacement.RELATED)
                                 .addComponent(getBtnIncluir()))
               );
      contentPane.setLayout(gl_contentPane);
      setVisible(true);
   }
}
